package com.sgb.kjwl.view.ui.activity.self.shop;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import com.sgb.kjwl.databinding.ActivityShopMyPriceBinding;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.fragment.shop.ShopMyPriceFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 我的报价界面
 */
public class ShopMyPriceActivity extends AppActivity {
//    邀请报价    已报价    已失效
    public static final int TAB_TO_BE_QUOTED = 1;
    public static final int TAB_QUOTED = 2;
    public static final int TAB_EXPIRED = 4;
    private ActivityShopMyPriceBinding binding;
    private String[] titles = {"邀请报价", "已报价", "已失效"};

    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context,Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, ShopMyPriceActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().postSticky(this);
    }
    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_shop_my_price);
        binding.backView.setOnClickListener(this);
        binding.searchShopPrice.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_SEARCHSHOPPRICE).setMessageData(binding.searchShopPrice.getText().toString()));
                }
                return false;
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
        }
    }
    @Override
    protected void init() {
        if (mFragments == null) {
            mFragments = new ArrayList<>();
        }
        mFragments.add(ShopMyPriceFragment.newInstance(TAB_TO_BE_QUOTED));
        mFragments.add(ShopMyPriceFragment.newInstance(TAB_QUOTED));
        mFragments.add(ShopMyPriceFragment.newInstance(TAB_EXPIRED));
        SlidingFragmentPagerAdapter mAdapetr = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewPager.setAdapter(mAdapetr);
        binding.viewPager.setOffscreenPageLimit(3);
        binding.tabLayout.setupWithViewPager(binding.viewPager);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
